/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package web;

import ejb.entity.Compte;
import ejb.entity.Operations;
import ejb.entity.OperationsBatch;
import ejb.facade.CompteFacadeRemote;
import ejb.facade.OperationsBatchFacadeRemote;
import ejb.facade.OperationsFacadeRemote;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Collection;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Trasheur
 */
@WebServlet(name = "ListOperationsBatch", urlPatterns = {"/ListOperationsBatch"})
public class ListOperationsBatch extends HttpServlet {

    @EJB
    private OperationsFacadeRemote operationsFacade;
    @EJB
    private CompteFacadeRemote compteFacade;
    @EJB
    private OperationsBatchFacadeRemote operationsBatchFacade;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ListOperations</title>");
            out.println("</head>");
            out.println("<body>");

            if ((request.getParameter("id_compte_source") != null)) {
                Collection<OperationsBatch> op_batch = operationsBatchFacade.findAll();
                Integer id_compte_source = Integer.valueOf(request.getParameter("id_compte_source"));
                Compte compte_source = compteFacade.find(id_compte_source);
                out.println("<h1>" + compte_source.getTypeCompte() + " N° " + compte_source.getIdCompte() + "</h1>");
                out.println("<ul>");
                for (Operations operation : operationsFacade.findAllSource(compte_source.getIdCompte())) {
                    if (operationsBatchFacade.find(operation.getIdOperation()) == null) {
                        continue;
                    }
                    out.println("<li>");
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
                    String dateStr = simpleDateFormat.format(operation.getDateDeCreation());
                    out.println(dateStr + " - ");
                    out.println(operation.getTypeOperation());
                    out.println("<ul>");
                    out.println("<li>Montant : " + operation.getMontant() + " €</li>");
                    Compte compte_cible = compteFacade.find(operation.getIdCompteCible());
                    if (compte_cible != null) {
                        out.println("<li>A destination du " + compte_cible.getTypeCompte() + " N° " + compte_cible.getIdCompte() + "</li>");
                    }
                    out.println("</ul>");
                    out.println("</li>");
                }
                for (Operations operation : operationsFacade.findAllCible(compte_source.getIdCompte())) {
                    if (operationsBatchFacade.find(operation.getIdOperation()) == null) {
                        continue;
                    }
                    out.println("<li>");
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
                    String dateStr = simpleDateFormat.format(operation.getDateDeCreation());
                    out.println(dateStr + " - ");
                    out.println(operation.getTypeOperation());
                    out.println("<ul>");
                    out.println("<li>Montant : " + operation.getMontant() + " €</li>");
                    compte_source = compteFacade.find(operation.getIdCompteSource());
                    if (compte_source != null) {
                        out.println("<li>provenant du " + compte_source.getTypeCompte() + " N° " + compte_source.getIdCompte() + "</li>");
                    }
                    out.println("</ul>");
                    out.println("</li>");
                }
                out.println("</ul>");

            } else {
                response.sendRedirect("ListClient");
            }
            out.println("<a href='ListClient'>Retour à l'accueil</a>");
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
